package org.hq.security.config;

import com.alibaba.fastjson.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录成功返回结果
 * jso
 */
public class MyA implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        //用户信息
        Object Principal = authentication.getPrincipal();
        //用户凭证
        Object credentials = authentication.getCredentials();

        Map map =new HashMap() ;
       map.put("code",0);
       map.put("message","登录成功");
       map.put("data",Principal);

        //把对象转换为字符串
        String json = JSON.toJSONString(map);
        //设置返回类型
        response.setContentType("application/json;charset=utf-8");
        //返回结果
        response.getWriter().println(json);
    }
}
